package com.stars.distributed.schedule.client.demo.rocketmq;

import com.alibaba.fastjson.JSON;
import com.alibaba.rocketmq.client.exception.MQBrokerException;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.remoting.exception.RemotingException;
import com.jy.modules.rocketmq.producer.RocketMQProducer;
import com.stars.distributed.schedule.client.DistributedTaskExecutionResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

@Component
public class MQDistributedCallBackProducer {
    private static final Logger logger = LoggerFactory.getLogger(MQDistributedCallBackProducer.class);

    @Autowired
    private RocketMQProducer myDistributedMQCallbackInstance;


    /**
     * @param mqCallback mq回调地址：若是activeMQ则为destination，若是rocketMQ则为topic
     * @param distributedTaskExecutionResult 远程分布式任务执行结果
     */
    public void send(String mqCallback, DistributedTaskExecutionResult distributedTaskExecutionResult) {

        logger.info("topic: {}", "com-callback-topic-demo");
        SendResult sendResult = null;
        try {
            Message message = new Message(mqCallback, JSON.toJSONString(distributedTaskExecutionResult).getBytes("utf-8"));
            String key = UUID.randomUUID().toString();
            message.setKeys(key);
            logger.info("消息发送  bodyStr: " + message.getBody().length );
            sendResult = myDistributedMQCallbackInstance.send(message);

            logger.info("sendResult: keys" + key + ": " + sendResult==null?"": sendResult.toString());
        } catch (UnsupportedEncodingException e) {
            logger.error("CreditRocketMQProducer rocket client 不支持字符编码异常:  ",e);
        } catch (MQClientException e) {
            logger.error("CreditRocketMQProducer rocketMQClient exception: ",e);
        } catch (RemotingException e) {
            logger.error("CreditRocketMQProducer rocket clientMQ remotingException: ",e);
        } catch (MQBrokerException e) {
            logger.error("CreditRocketMQProducer rocket clientMQ BrokerException: ",e);
        } catch (InterruptedException e) {
            logger.error("CreditRocketMQProducer rocketMQ InterruptedException: ",e);
        }
    }

    public SendResult sendTest(String mqCallback, String body) {

        logger.info("topic: {}", "com-callback-topic-demo");
        SendResult sendResult = null;
        try {
            Message message = new Message(mqCallback, JSON.toJSONString(body).getBytes("utf-8"));
            String key = UUID.randomUUID().toString();
            message.setKeys(key);
            logger.info("消息发送 进行测试 bodyStr: " + message.getBody().length );
            sendResult = myDistributedMQCallbackInstance.send(message);

            logger.info("sendResult: keys" + key + ": " + sendResult==null?"": sendResult.toString());
        } catch (UnsupportedEncodingException e) {
            logger.error("CreditRocketMQProducer rocket client 不支持字符编码异常:  ",e);
        } catch (MQClientException e) {
            logger.error("CreditRocketMQProducer rocketMQClient exception: ",e);
        } catch (RemotingException e) {
            logger.error("CreditRocketMQProducer rocket clientMQ remotingException: ",e);
        } catch (MQBrokerException e) {
            logger.error("CreditRocketMQProducer rocket clientMQ BrokerException: ",e);
        } catch (InterruptedException e) {
            logger.error("CreditRocketMQProducer rocketMQ InterruptedException: ",e);
        }

        return sendResult;
    }

}